package com.ll.config;

import java.io.Serializable;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
/**
 * redis客户端配置
 * @author sunhuaili
 *
 */
@Configuration
public class RedisSpringbootConfiguration {
	
	/**
	 * 创建一个操作redis的客户端对象
	 * @param factory
	 * @return
	 */
	@Bean
	public <K extends Serializable, V extends Serializable> RedisTemplate<K, V> redisTemplate(
			RedisConnectionFactory factory) {
		RedisTemplate<K, V> template = new JdkRedisTemplate<K, V>();
		template.setConnectionFactory(factory);
		return template;
	}

	private static class JdkRedisTemplate<K, V> extends RedisTemplate<K, V> {
		public JdkRedisTemplate() {
			JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
			setKeySerializer(jdkSerializationRedisSerializer);
			setValueSerializer(jdkSerializationRedisSerializer);
			setHashKeySerializer(jdkSerializationRedisSerializer);
			setHashValueSerializer(jdkSerializationRedisSerializer);
		}
	}
}
